package com.tensquare.qa.controller;

import com.tensquare.base.pojo.Label;
import com.tensquare.qa.client.LabelClient;
import com.tensquare.qa.pojo.Problem;
import com.tensquare.qa.service.ProblemService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author Macross
 * @Type ProblemController.java
 * @Desc 问题信息管理控制器
 * @date 2019/12/17 17:14
 */
@RestController
@RequestMapping("/problem")
@Api(description = "问题信息管理控制器")
public class ProblemController {

    @Autowired
    private ProblemService problemService;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private LabelClient labelClient;

    /**
     * 获取最新问题信息列表
     * @param labelId 标签id
     * @param page 分页页码
     * @param size 分页大小
     * @return
     */
    @GetMapping("/newlist/{labelId}/{page}/{size}")
    @ApiOperation(value = "获取最新的问题信息列表",notes = "按问题创建时间进行降序排序。返回的问题信息列表经过分页处理")
    public Result<PageResult<Problem>> getNewList(@PathVariable @ApiParam(value = "标签id",required = true) String labelId,
                                            @PathVariable @ApiParam(value = "分页页码",required = true) Integer page,
                                            @PathVariable @ApiParam(value = "分页大小",required = true) Integer size){
        //获取最新回答信息列表
        PageResult<Problem> result = problemService.getNewList(labelId,page,size);
        //将获取结果进行封装并返回
        return new Result<>(true,StatusCode.OK,"查询成功",result);
    }

    /**
     * 获取热门问题信息列表
     * @param labelId 标签id
     * @param page 分页页码
     * @param size 分页大小
     * @return
     */
    @GetMapping("/hotlist/{labelId}/{page}/{size}")
    @ApiOperation(value = "获取热门的问题列表",notes = "按点击数进行降序排序。返回的问题信息列表经过分页处理")
    public Result<PageResult<Problem>> getHotList(@PathVariable @ApiParam(value = "标签id",required = true) String labelId,
                                                  @PathVariable @ApiParam(value = "分页页码",required = true) Integer page,
                                                  @PathVariable @ApiParam(value = "分页大小",required = true) Integer size){
        //获取热门回答信息列表
        PageResult<Problem> result = problemService.getHotList(labelId,page,size);
        //将获取结果进行封装并返回
        return new Result<>(true,StatusCode.OK,"查询成功",result);
    }

    /**
     * 添加问题
     * @param problem 问题信息对象
     * @return
     */
    @PostMapping
    @ApiOperation(value = "添加问题",notes = "只有在登录后才可以添加问题")
    public Result addProblem(@RequestBody @ApiParam(value = "问题信息",required = true) Problem problem){
        //判断request中是否有claims对象
        if (request.getAttribute("claims") != null) {
            //获取claims对象
            Claims claims = (Claims) request.getAttribute("claims");
            //获取用户id
            String id = claims.getId();
            //设置用户id
            problem.setUserId(id);
            //添加问题
            problemService.add(problem);
            //返回
            return new Result(true,StatusCode.OK,"操作成功");
        }
        //返回
        return new Result(false,StatusCode.ACCESS_ERROR,"无权添加");
    }

    /**
     * 获取标签信息
     * @param id 标签ID
     * @return
     */
    @GetMapping("/label/{id}")
    @ApiOperation(value = "获取标签信息",notes = "通过远程调用获取标签信息")
    public Result<Label> findById(@PathVariable @ApiParam(value = "标签ID",required = true) String id){
        return labelClient.findById(id);
    }
}
